<template>
  <div>

    <div>

    </div>


  </div>

</template>

<script>
import {optionsSex} from "@src/common/utils/constant";

export default {
  name: "items",
  props: {
    show: {
      type: Boolean,
      default: false
    },
    actionType: {
      type: String,
      default: ''
    },
    group:{
      type: Object,
      default: {},
    }
  },
  watch: {
    group(newValue, oldValue) {
      this.groupData = newValue
      this.queryExamItemList()
      this.medicalDetail()
      this.$forceUpdate()
    },
    show(newValue, oldValue) {
      this.dialogItems = newValue
    },
  },
  data() {
    return {
      optionsSex:optionsSex,
      examItemSearchForm: {},
      packageSearchForm: {
        applyTo:this.group.groupIndx
      },
      packageSel: {
        packageName:"",
        price:0,
      },
      staffList: [],
      staffListInit: [],
      staffData: [],
      selectedStaffData: [],
      selectedStaffList:  [],
      selectedStaffListInit:  [],
      selectedLength:0,
      packageList: [],
      originalprice: 0,
      dialogPackage: false,
      dialogItems: this.show,
      groupData: this.group,
      action: this.actionType,
      res:{}
    }
  },
  filters: {
    formatContractStatus: function (val) {
      var dict = {}
      contractStatus.forEach(v => {
        dict[v.value] = v.label
      })

      return dict[val]

    },
    formatSex: function (val) {
      var dict = {1: "男", 2: "女"}
      return dict[val]
    },
    formatType: function (val) {
      var dict = {1: "普通体检", 2: "单位体检", 3: "入职体检", 4: "职业体检", 5: "婚育体检", 6: "老年专项体检", 7: "健康体检等"}
      return dict[val]
    },
    formatStatus: function (val) {
      var dict = {1: "未登记", 2: "登记", 4: "检中", 8: "交表", 16: "初检", 32: "复检", 64: "打印"}
      return dict[val]
    },
    formatDate: function (time) {
      if (time != null && time !== "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd hh:mm");
      } else {
        return "";
      }
    },
    formatApplyTo: function (val) {
      const dict = {}
      optionsSex.forEach(v => {
        dict[v.value] = v.label
      })
      if(dict[val] !== undefined){
        return dict[val]
      }else {
        return val
      }

    }
  },
  created() {
    console.log('items created')
    this.queryExamItemList()
    this.medicalDetail()
  },
  methods: {
    queryExamItemList() {
      const params = {...this.examItemSearchForm,pageNo:1,pageSize:999999999}
      this.$axios.examItemList(params).then(res => {
        if (res.code === '00000') {
          const {records} = res.data

          this.staffListInit = records.slice()
          this.staffList = records
        }
      })
    },
    // 已保存项目明细
    medicalDetail() {
      if(this.groupData.id){
        console.log('this.groupData:',this.groupData)
        this.$axios.getGroupTemplateInfo({
          id: this.groupData.id
        }).then(res => {
          if (res.code === '00000') {
            const {peContractsGroupItemTemplates} = res.data
            this.selectedStaffListInit = peContractsGroupItemTemplates.slice()
            this.selectedStaffList = peContractsGroupItemTemplates
            this.getSummaries();
          }
        })
      }
    },

    handleStaffChange(rows) {
      this.staffCheckNum = rows.length
      this.staffData = []
      if (rows) {
        rows.forEach((row) => {
          if (row) {
            this.staffData.push(row)
          }
        })
      }
    },

    getRowKey(row) {
      return row.itemId
    },
    // 左->右(单选-全选)
    addStaff(type) {
      setTimeout(() => {
        // 清空选择
        this.$refs['staffTable'].clearSelection()
        this.$refs['selectedStaffTable'].clearSelection()
      }, 0)
      if (type === 'single') {
        this.staffData.forEach((item) => {
          this.selectedStaffList.push(item)
        })
        this.selectedStaffNum = this.selectedStaffList.length
        for (let i = 0; i < this.staffList.length; i++) {
          for (let j = 0; j < this.staffData.length; j++) {
            if (this.staffList[i].id === this.staffData[j].id) {
              this.staffList.splice(i, 1)
              this.staffNum = this.staffList.length
            }
          }
        }
      }
      this.getSummaries();
    },
    // 右边表格选中存入selectedStaffData
    handleSelectedStaffChange(rows) {
      this.selectedStaffCheckNum = rows.length
      this.selectedStaffData = []
      if (rows) {
        rows.forEach((row) => {
          if (row) {
            this.selectedStaffData.push(row)
          }
        })
      }
    },
    // 右->左(单选-全选)
    removeStaff(type) {
      setTimeout(() => {
        this.$refs['staffTable'].clearSelection()
        this.$refs['selectedStaffTable'].clearSelection()
      }, 0)
      if (type === 'single') {
        this.selectedStaffData.forEach((item) => {
          this.staffList.push(item)
          this.staffNum = this.staffList.length
        })
        // console.log('this.staffList', this.staffList)
        for (let i = 0; i < this.selectedStaffList.length; i++) {
          for (let j = 0; j < this.selectedStaffData.length; j++) {
            if (
                this.selectedStaffList[i] &&
                this.selectedStaffData[j] &&
                this.selectedStaffList[i].id === this.selectedStaffData[j].id
            ) {
              this.selectedStaffList.splice(i, 1)
              this.selectedStaffNum = this.selectedStaffList.length
            }
          }
        }
      }
      if (type === 'all') {
        this.selectedStaffList.forEach((item) => {
          this.staffList.push(item)
          this.staffNum = this.staffList.length
        })
        this.selectedStaffList = []
        this.selectedStaffNum = 0
      }
      this.getSummaries();
    },
    // 合计方法
    getSummaries() {
      let totalPrice = 0.00
      this.selectedStaffList.forEach((item) => {
        if (item.price == null) {
          item.price = 0;
          totalPrice += parseFloat(item.price);
        } else {
          totalPrice += parseFloat(item.price);
        }
      });
      this.originalprice = totalPrice.toFixed(2)
    },
    // 选择套餐
    setmeal() {
      this.dialogPackage = true;
      this.readPackageList();  //这里改成根据分组id获取套餐
    },
    readPackageList() {
      const params = {...this.packageSearchForm ,pageNo:1,pageSize:999999999}
      this.$axios.queryPbPackagesList(params).then(res => {
        if (res.code === '00000') {
          this.packageList = res.data.records
        }
      })
    },

    selectPackage(selection, row) {
      this.packageSel = row;
      if (selection.length > 1) {
        let del_row = selection.shift()
        this.$refs.multipleTable1.toggleRowSelection(del_row, false) // 用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
      }
    },
    // 储存选择的套餐
    savemealis() {
      this.$axios.queryPackagItems({
        "packageId": this.packageSel.id, pageNo:1, pageSize:999999999
      }).then(res => {
        if (res.code === '00000') {
          let arr = res.data.examItemsDTOList;
          if (this.selectedStaffList.length === 0) {
            this.selectedStaffList = arr
          } else {
            this.selectedStaffList = this.selectedStaffList.concat(arr);
          }
          this.selectedStaffList.map(v => {
            return {
              ...v,
              postType: "单位",
              payStatus: 1,
            }
          })
          this.getSummaries()
        }
      })

      this.dialogPackage = false;
    },
    sendtotal() {
      let items = []
      if(this.selectedStaffList.length===0){
        this.$message.error("请添加体检项目")
        return
      }
      this.selectedStaffList.forEach(item => {
        if (item.id != null) {
          items.push(
              {
                "applyTo": item.applyTo,
                "groupId": item.groupId,
                "itemId": item.id,
                "price": item.price,
                // "sequence": "显示序号"
              }
          )
        }
      })
      let obj = {
        "id": this.groupData.id,
        "packageName": this.packageSel.packageName,
        "packagePrice": this.packageSel.price,
        "discountPrice": this.originalprice,
        "peContractsGroupItemTemplates": items
      }

      this.$axios.updateGroupTemplateList(obj).then(res => {
        if (res.code === '00000') {
          this.$message.success("提交成功")
          this.close()
        }
      })
    },
    close(){
      this.dialogItems = false
      const res = {...this.res, show:false}
      this.$emit("change", res);
    }
  }
}
</script>

<style scoped>
.info_sel {
  display: flex;
  width: 100%;
}

.info_sel_l {
  width: 50%;
}

.info_sel_r {
  width: 50%;
  margin-left: 50px;
}

.list {
  display: flex;
}

/* .info_r{
	margin-left: 10px;
	width: ;
} */
.dwflex {
  display: flex;
}

.dwflex > div {
  width: 50%;
}

.newunit {
  margin-bottom: 20px;
  margin-left: 17%;
  width: 150px;
  color: black;
}

.ifunit /deep/ .el-input {
  width: 40%;
  margin-right: 5%;
}

.top_dialog /deep/ .el-dialog {
  margin-top: 3vh !important;
}

.el-select {
  width: 100%;
}

.el-select /deep/ .el-input__inner {
  width: 100%;
}

::v-deep .input_c .el-input__inner {
  width: 100%;
}

::v-deep .el-dialog__body {
  border-bottom: 1px solid #cccccc;
  margin: 0 20px;
  padding: 20px 20px 0px;
  /* padding: 20px; */
}

.date {
  text-align: right;
  color: #7F7F7F;
  font-weight: bold;
}

.info_l {
  /* width: 80%; */
  margin: 0px auto;
}

.info_l_input {
  width: 82%;
}


.info_sel_r_groupn {
  margin-left: 40px;
}

.form-row {
  overflow: hidden;
}

.form-col {
  float: left;
}


.info_sel_r_img {
  width: 180px;
  height: 220px;
  margin: 0px auto;
}

.avatar-uploader {
  margin: 0px auto;
  text-align: center;
}

.info_l /deep/ .el-select {
  width: 100%;
}

.info_l_head {
}

.el-table th > .cell {
  white-space: nowrap;
}


.tan_k {
  display: flex;
  justify-content: space-between;
}

.textarea {
  width: 350px;
  height: 120px;
}

.el-textarea /deep/ .el-textarea__inner {
  min-height: 89px;
  height: 89px;
}

.text_a {
  width: 50%;
  margin-left: 20px;
}

.text_a > span {
  width: 100%;
  display: inline-block;
  border-bottom: 1px solid #cccccc;
}

.add_jian {
  padding-top: 15px;

}

.add_jian > span {
  display: block;
  color: #3298D3;
  font-size: 8px;
  line-height: 24px;
}

::v-deep .el-dialog__body {
  border: 0px;
}

.tableTitle {
  padding-top: 10px;
}

.text-center {
  text-align: center !important;
}

.text-left {
  text-align: left !important;
}

.bg-blue {
  height: 500px;
  background-color: #CFE8F4;
  border: 1px solid #CFE8F4;
  overflow-y: auto;
}

.bg-grey {
  background-color: #F2F2F2;
}

.buttonAll {
  padding-bottom: 50px;
}

.text-red {
  color: #D9001B;
}

.mt-20 {
  margin-top: 20px;
}

.pt-20 {
  padding-top: 20px;
}

.pb-20 {
  padding-bottom: 20px;
}

.mar-10 {
  padding: 0 10px;
}

.check-style {
  margin: 10px 0 0 40px !important;
}

.staffButton {
  margin-top: 20px;
}

.nsm-right-top {
  display: inline;
  margin-left: 10px;
}

.pagination {
  text-align: center;
}

.nsm-bottom-line {
  margin-top: 5px;
  margin-bottom: 20px;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  background-color: #fff;
}

.vmm {
  margin-top: 10px;
}

.disflex {
  display: flex;
  justify-content: space-between;
}

.main_box_title {
  background: #fff;
  padding: 10px;
}

.aside_box_title {
  margin: 0px 8px 10px;
  /* text-align: center; */
}

.header_top .el-divider--horizontal {
  margin-top: 0px;
}

.add_info .el-divider--horizontal {
  margin-top: 10px;
}

.add_info {
  display: flex;
  padding: 0px 20px;
}

.el-main {
  padding: 0px;
  padding-left: 20px;
}

.staff_form_btn {
}

.info_l {
  text-align: left;
  flex: 1;
}

.info_r {
  text-align: left;
  flex: 1;
  margin-left: 30px;
}

.info_l_input {
  width: 82%;
}

.info_l_button {
  margin-left: 10px;
}

.info_sel_r_groupn {
  margin-left: 40px;
}

.info_sel_r_group {
  margin-left: 10px;
}

.info_sel {
  display: flex;
}

.info_sel_l {
  width: 50%;
}

.info_sel_r {
  width: 50%;
  margin-left: 50px;
}

.info_sel_r_img {
  width: 180px;
  height: 220px;
  margin: 0px auto;
}

.avatar-uploader {
  margin: 0px auto;
  text-align: center;
}

.info_l /deep/ .el-select {
  width: 100%;
}

.info_l_head {
}

.pagination {
  text-align: center;
}

.top_dialog /deep/ .el-dialog {
  margin-top: 3vh !important;
}

.photog {
  width: 600px;
  height: 400px;
  margin: 10px auto;
}

.photog video {
  width: 600px;
  height: 400px;
}

.photog canvas {
  width: 600px;
  height: 400px;
}

.info_sel_s_butn {
  margin: 0px auto;
}

.info_sel_butn {
  margin-left: 20px;
}

.info_sel_s {
  margin-left: 20px;
}

.ldog_butn {
  width: 120px;
  height: 50px;
  margin: 0px auto;
}

.form_div {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  padding: 0px 30px;
}

.block {
  text-align: center;
}

.lai_div {
  margin-top: 20px;
  display: flex;
}

.lai_div_l {
  width: 480px;
}

.lai_div_l /deep/ .el-input--suffix {
  width: 400px;
}

.lai_div_r {
  margin-left: 20px;
}

.yvyue_div {
  display: flex;
  justify-content: space-between;
}

.yvyue_div button {
  width: 150px;
}

.options {
  position: relative;
}

.options_div {
  position: absolute;
  top: 5px;
  right: 10px;
  z-index: 99 !important;
}

/* 扫码交表 */
.smjb-top {
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}

.smjbtablestyle {
  margin: auto;
}

.title1 {
  margin: 20px auto;
  width: 80%;
  font-size: 20px;
}

.smfooter {
  display: flex;
  justify-content: space-around;
  padding-top: 20px;
  font-size: 12px;
  color: #D9001B;
  border-top: 1px solid #eee;
}

.dfooter {
  padding-top: 30px;
  display: flex;
  justify-content: space-between;
}

.zyd-code {
  font-size: 28px;
}

.zyd-title {
  font-size: 32px;
  text-align: center;
}

.zyd-item {
  margin-top: 20px;
  font-size: 18px;
}

.fontstylekai, .adjusttable {
  font-family: "楷体";
}

.title4 {
  margin-top: 20px;
  padding: 15px 0;
  text-align: center;
  background-color: #f0f0f0;
}

.adjusttable {
  width: 100%;
  font-size: 18px;
}

.adjusttable td {
  padding: 20px 0;
  border-bottom: 1px solid #999;
}

.adjusttable td:nth-child(2) {
  width: 60%;
}

.adjusttable .el-checkbox__label {
  font-size: 18px !important;
}

.title-common {
  position: relative;
  padding: 30px 0;
  text-align: center;
  font-size: 26px;
  font-weight: normal;
}

.title-common span {
  position: absolute;
  right: 0;
}

/* 打印表单样式 */
.printinput /deep/ .el-input__inner {
  border: 0;
  border-bottom: 1px solid #333;
  border-radius: 0;
}

.jfnoticebottom {
  display: flex;
  margin: 20px 0 40px;
}

.jfnoticebottom span {
  flex: 1;
}
</style>
